package com.algorithm;

/**
 * @author: sun.hongliang
 * @create: 2019/07/05 12:17
 */
public class LongestPalindromeTest {
    public static void main(String[] args) {
        String s = "babadada";
        final String s1 = longestPalindrome(s);
        System.out.println(s1);
    }

    public static String longestPalindrome(String s) {
        final char[] chars = s.toCharArray();
        final int length = s.length();
        String sb = "";
        for (int i = 0; i < length; i++) {
            int end = length - 1;
            boolean flg = false;
            for (int j = length - 1; j > i; j--) {
                if (chars[i] == chars[j]) {
                    int start = i;
                    end = j;
                    int k = j;
                    boolean temp = true;
                    while (start <= k) {
                        if (chars[start++] != chars[k--]) {
                            temp = false;
                            break;
                        }
                    }
                    if (temp) {
                        flg = temp;
                        break;
                    }
                }
            }
            if (flg && end - i > sb.length() - 1) {
                sb = s.substring(i, end + 1);
            }
        }
        if (sb.length() == 0 && s.length() > 0) {
            return String.valueOf(s.charAt(0));
        }
        return sb;
    }
}
